/**
 * 银行柜员仿真
 *
 * @author fireway
 * @since 2019年 03月 17日 星期日 06:50:49 CST
 */
package concurrency;

import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class BankTellerSimulation {
    private static final int MAX_LINE_SIZE = 50;

    private static final int ADJUSTMENT_PERIOD = 1000;

    public static void main(String[] args) {
        ExecutorService exec = Executors.newCachedThreadPool();

        // If line is too long, customers will leave:
        CustomerLine customers = new CustomerLine(MAX_LINE_SIZE);
        exec.execute(new CustomerGenerator(customers));

        // Manager will add and remove tellers as necessary:
        exec.execute(new TellerManager(exec, customers, ADJUSTMENT_PERIOD));

        if(args.length > 0) {
            try {
                TimeUnit.SECONDS.sleep(new Integer(args[0]));
            } catch (NumberFormatException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        } else {
            System.out.println("Press 'Enter' to quit");
            try {
                System.in.read();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        exec.shutdownNow();
    }
}
